package cn.hl.hm.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vizhuo.smscode.model.SMSCode;

import cn.hl.hm.dao.RoadFeeCollectorMapper;
import cn.hl.hm.entity.RoadFeeCollector;
import cn.hl.hm.service.SMSCodeIService;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.Result;
import cn.hl.hm.util.Tools;

@Controller
@RequestMapping("/roadFeeCollectorController")
public class RoadFeeCollectorController {
	private static final Logger logger = LoggerFactory.getLogger(DriverController.class);
	
	 @Autowired
	    SMSCodeIService sMSCodeIService;
	@Autowired
	RoadFeeCollectorMapper rfcMapper;
	
	
	 /**
	  * @Title: regist  
	  * @Description: 添加收费员
	  * @date:2017年3月17日10:34:15
	  * @author: 星矢
	  * @param DriverAccount
	  * @param verifyCode
	  * @param platformId
	  * @param password
	  * @param invitationCode
	  * @param request
	  * @param response
	  * @return
	  * @throws IOException String 
	  * @throws
	 */
	@RequestMapping(value = "/addRoadFeeCollector", method = RequestMethod.POST)
	public String addRoadFeeCollector( RoadFeeCollector rfc,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("配载员---添加---roadFeeCollectorController/addRoadFeeCollector");
		request.setCharacterEncoding("UTF-8");
		try {
			rfc.setCreateTime(new Date().toLocaleString());
			RoadFeeCollector r = new RoadFeeCollector();
			r.setCollectorAccount (rfc.getCollectorAccount());
			List<RoadFeeCollector> rL= rfcMapper.selectRoadFeeCollector(r);
			if(rL==null || rL.size()==0 ){
				int add = rfcMapper.insertSelective(rfc);
				if (add > 0) {
					Result.ok(null, InfoCode.SUCCESS, response);
				} else {
					Result.ok(null, InfoCode.FAILURE, response);
				}
			}else{
				Result.ok(null, InfoCode.RFC_ERROR, response);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
		}
		return null;
	}
	
	/**
	  * @Title: regist  
	  * @Description: 配载费管理后台——删除收费员信息
	  * @date:2017年3月17日10:34:15
	  * @author: 星矢
	  * @param DriverAccount
	  * @param verifyCode
	  * @param platformId
	  * @param password
	  * @param invitationCode
	  * @param request
	  * @param response
	  * @return
	  * @throws IOException String 
	  * @throws
	 */
	@RequestMapping(value = "/deleteRoadFeeCollector", method = RequestMethod.POST)
	public String deleteRoadFeeCollector( Integer id,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("配载员---删除---roadFeeCollectorController/deleteRoadFeeCollector");
		request.setCharacterEncoding("UTF-8");
		try {
			int delete = rfcMapper.deleteByPrimaryKey(id);
			if (delete > 0) {
				Result.ok(null, InfoCode.SUCCESS, response);
			} else {
				Result.ok(null, InfoCode.FAILURE, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
		}
		return null;
	}
	@RequestMapping(value = "/selectRoadFeeCollector", method = RequestMethod.POST)
	public String selectStRouteInfo( RoadFeeCollector rfc,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("配载员---查询---roadFeeCollectorController/selectRoadFeeCollector");
		request.setCharacterEncoding("UTF-8");
		try {
		 
			List<RoadFeeCollector> rL= rfcMapper.selectRoadFeeCollector(rfc);
			if(rL!=null || rL.size()>0 ){
					Result.ok(rL, InfoCode.SUCCESS, response);
			}else{
				Result.ok(null, InfoCode.FAILURE, response);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
		}
		return null;
	}
	
	/**
     * 用户端登录 
     *
     * @param userAccount
     * @param verifyCode
     * @param type
     * @param request
     * @param response
     * @return
     * @throws ControllerException
     * @throws UnsupportedEncodingException 
     */
    @RequestMapping(value = "/checkSMSCode", method = RequestMethod.POST)
    public  String checkSMSCode(@RequestParam String collectorAccount,
                         @RequestParam String verifyCode,
                         HttpServletRequest request,
                         HttpServletResponse response) throws IOException{
    	logger.info("配载员---登录---roadFeeCollectorController/checkSMSCode");
        request.setCharacterEncoding("UTF-8");
        //1.接收参数验证是否为空
    	//判断用户手机号是否为空
        if (Tools.isEmpty(collectorAccount)
                || Tools.isEmpty(verifyCode)
                 ) {
        	Result.ok(null, InfoCode.PARAM_ERROR, response);
            return null;
        }
        //2.判断短信验证码是否正确。
        SMSCode sMSCode = new SMSCode(); 
        sMSCode.setMobile(collectorAccount);
        sMSCode.setCode(verifyCode);
        
        System.out.print("-----------11:"+verifyCode);
        // 测试时 注释掉
        SMSCode smsCode = sMSCodeIService.findCode(sMSCode);
        System.out.print("-----------smsCode:"+smsCode);
        
        if (null != smsCode) {//短信验证码是否正确！
        	RoadFeeCollector rfc = new RoadFeeCollector();
        	rfc.setCollectorAccount(collectorAccount);
        	List<RoadFeeCollector> rL= rfcMapper.selectRoadFeeCollector(rfc);
			if(rL!=null || rL.size()>0 ){
					Result.ok(rL.get(0), InfoCode.SUCCESS, response);
			}else{
				Result.ok(null, InfoCode.RESET_ERROR, response);
			}
        	Result.ok(null, InfoCode.SUCCESS, response);
			return null;
        }else{
        	Result.ok(null, InfoCode.MESSAGE_ERROR, response);
            return null;
  	  	}
    }
	
}
